<?php

$file  = file('svn.log');
$items = array();
$tmp   = array();
foreach ($file as $line) {
    if (substr(trim($line), 0, 5)=='-----') {
        if (!empty($tmp)) {
            $items[] = $tmp;
        }
        $tmp = array();
    } else {
        if (strlen(trim($line))>0) {
            $tmp[] = trim($line);
        }
    }
}
$template = array(
    'revision'  => null,
    'bug_no'    => '',
    'created'   => '',
    'developer' => '',
    'comment'   => ''
);
$svn = array();
foreach ($items as $rows) {
    $line = $template;
    $line['comment'] = trim($rows[1]);
    extractLine($rows[0], $line);
    $line['bug_no'] = extractBugzillaTicket($rows[1]);
    $svn[$line['revision']] = $line;
}

$dquote = '"';

$csv = $dquote.join($dquote."\t".$dquote, array_keys($template)).$dquote."\n";

foreach ($svn as $id => $row) {
    $csv .= $dquote.join($dquote."\t".$dquote, $row).$dquote."\n";
}

file_put_contents('/tmp/craig.csv', $csv);




function extractBugzillaTicket($line, $prefix='#')
{
    $line = trim($line);
    if (is_bool(strpos($line, $prefix))) {
        return 'n/a';
    } else {
        $pos = strpos($line, $prefix);
        $tmp = substr($line, $pos, strlen($line)-$pos);
        $expl = @explode(' ', $tmp);
        return trim($expl[0]);
    }
}

function extractLine($line, &$target)
{
    $line = trim($line);
    if ($line[0]!='r') {
        return;
    }
    $expl = explode('|', $line);
    
    // revision
    $rev = trim($expl[0]);
    if (substr($rev, 0, 1)=='r') {
        $rev = (int) substr($rev, 1, strlen($rev)-1);
    }
    $target['revision'] = $rev;
    // developer
    $target['developer'] = trim($expl[1]);
    // developer
    $target['created'] = trim(substr($expl[2], 0, 20));
    return;
}




/**
 * Creates output of the subject passed, by default halting further execution
 * 
 * @param mixed $subject Element that you need insight into
 * 
 * @param bool  $halt    Flag (optional) if TRUE execution halts after dump
 * 
 * @return void
 */
function trap($subject, $halt=true)
{
    $cli = (isset($_SERVER['TERM'])) ? true : false;
    
    try {
        throw new Exception('DUMMIE');
    } catch (Exception $xc) {
    }
    
    ob_start();
    var_dump($subject);
    $dump = ob_get_clean();
    
    $details = array(
        'Details'     => print_r($subject, true),
        'Dump'        => $dump,
        'Stack trace' => print_r($xc->getTrace(), true)
    );
    
    echo ($cli!==true) ? "<pre>" : chr(27)."[H".chr(27)."[2J"."\n";
    
    foreach ($details as $descr => $data) {
        
        if (true===$cli) {
            echo "\n\n------------- {$descr} -----------------\n\n{$data}\n";
        } else {
            echo "<h3>{$descr}</h3><br>{$data}<br>";
        }
    }
    
    echo ($cli!==true) ? "<pre>" : "\n";
    
    if ($halt===true) {
        exit();
    }
}